//import fs from 'fs';
import fs from 'fs/promises';

//fs.readFile('./test.txt','utf8',(err,data)=>{
//if(err)throw err;
//console.log("Callback版本:");
//   console.log(data);
//    console.log("========================================");
//});

//const data =fs.readFileSync('./test.txt','utf8');
//console.log("同步版本:");
//console.log(data);
fs.readFile('./test.txt','utf8')
.then(data=>{
    console.log(data);
console.log("========================");
}).catch(err =>{
    console.error(err);
});

const readFile = async()=>{
    try{
        const data = await fs.readFile('./test.txt','utf8');
        console.log("Async/Await 版本:");
        console.log(data);
        console.log("========================");
    }catch(err){
        console.error(err);
    }
}

const writeFile= async()=>{
    try{
        await fs.writeFile('./test.txt','写入文件内容','utf8');
        console.log("写入成功");
    }catch(err){
        console.error(err);
    }
};

const appendFile = async()=>{
    try{
        await fs.appendFile('./test.txt','\n这是追加文件内容','utf8');
        console.log("追加成功");
    }catch(err){
        console.error(err);
    }
};
writeFile();
appendFile();
readFile();